require 'feedzirra'

class RssSource < Source
  def fetch
    super
    
    # load the items
    feed = Feedzirra::Feed.fetch_and_parse(url)

    # save the max latest_time (this is in case the feed entries are not sorted by date)
    old_latest = latest_authored_at.dup rescue nil
    
    fetched = 0
    
    # for each item that we don't have already, enqueue it
    if feed.respond_to?(:entries)
      feed.entries.reverse.each do |e|
        # be safe!
        e.sanitize!
      
        # if the post has a date, compare it
        if e.published
          # if this post is newer, than previous, enqueue it
          if old_latest.nil? || e.published > old_latest
            enqueue_entry(e)
            fetched += 1
          end
        # otherwise we have to check the digest
        else
          digest = Digest::SHA1.hexdigest(YAML::dump(e))
          unless reports.find_by_digest(digest)
            enqueue_entry(e)
            fetched += 1
          end
        end
      end
    else
      add_error("Error fetching feed")
    end
    
    # save in order to store the latest_authored_at time
    save
    
    update_deadline
    
    return fetched
  end
  
  def default_update_freq
    300
  end
  
  # writes a feed entry (as returned from feedzirra) to disk
  def enqueue_entry(entry)
    # pick a random ID and enqueue
    rand_id = (1000000000 + rand(9000000000)).to_s
    enqueue(rand_id, YAML::dump(entry))
    
    # update the latest_authored_at date if this post is newer
    self.latest_authored_at = entry.published if latest_authored_at.nil? || entry.published > latest_authored_at
  end
  
  def parse_one
    # get the YAML
    yaml = dequeue
    return false if yaml.nil?

    # decode
    entry = YAML::load(yaml)
    
    # copy attribs & set authored_at and digest
    attribs = {}; %w[title url author content].each{|k| attribs[k.to_sym] = entry.send(k)}
    attribs[:content] = entry.summary if attribs[:content].blank?
    attribs[:authored_at] = entry.published
    attribs[:digest] = Digest::SHA1.hexdigest(yaml)
    
    # create report
    create_report(attribs)
    
    return true
  end
end